package com.toy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.toy.entity.OrderDetail;
import com.toy.entity.Orders;
import com.toy.entity.Product;
import com.toy.entity.User;
import com.toy.mapper.OrderDetailMapper;
import com.toy.mapper.OrdersMapper;
import com.toy.mapper.ProductMapper;
import com.toy.mapper.UserMapper;
import com.toy.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toy.vo.OrderDetailVO;
import com.toy.vo.OrderListVO;
import com.toy.vo.OrdersVO;
import com.toy.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-08-31
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    private static final Integer ORDER_STATUS_INFO = 0;

    @Override
    public List<OrdersVO> findAllByUserId(Integer id) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        List<Orders> ordersList = this.ordersMapper.selectList(queryWrapper);
        List<OrdersVO> ordersVOList = new ArrayList<>();
        for (Orders orders : ordersList) {
            OrdersVO ordersVO = new OrdersVO();
            BeanUtils.copyProperties(orders, ordersVO);
            QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("order_id", orders.getId());
            List<OrderDetail> orderDetailList = this.orderDetailMapper.selectList(queryWrapper1);
            List<OrderDetailVO> orderDetailVOList = new ArrayList<>();
            for (OrderDetail orderDetail : orderDetailList) {
                OrderDetailVO orderDetailVO = new OrderDetailVO();
                BeanUtils.copyProperties(orderDetail, orderDetailVO);
                if (orderDetailVO.getOrderId().equals(ordersVO.getId())){
                    ordersVO.setStatus(orderDetailVO.getStatus());
                }
                Product product = this.productMapper.selectById(orderDetail.getProductId());
                BeanUtils.copyProperties(product, orderDetailVO);
                orderDetailVOList.add(orderDetailVO);
            }
            ordersVO.setOrderDetailVOList(orderDetailVOList);
            ordersVOList.add(ordersVO);
        }

        return ordersVOList;
    }

    @Override
    public Boolean updateStatusBySerialnumber(String serialnumber) {
        Integer idBySerialnumber = ordersMapper.findIdBySerialnumber(serialnumber);
        if (idBySerialnumber == null){
            System.out.println("=============订单ID未找到===========");
            return false;
        }
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(idBySerialnumber);
        orderDetail.setId(idBySerialnumber);
        orderDetail.setStatus(1);
        int i = orderDetailMapper.updateById(orderDetail);
        if (i != 1){
            System.out.println("=============状态修改失败===========");
            return false;
        }
        return true;
    }

    @Override
    public PageResult findOrderByPage(PageResult pageResult) {
        // 定义分页对象，封装2个参数 public Page(long current, long size)
        IPage<Orders> page = new Page<>(pageResult.getPageNum(),
                pageResult.getPageSize());
        // 定义查询条件
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "login_name", pageResult.getQuery());

        // 根据传入的分页条件，和查询条件，获得 分页结果数据
        //   （4个参数：current size total records查询记录的结果）
        page = ordersMapper.selectPage(page, queryWrapper);

        // 封装结果
        List<Orders> orderList = page.getRecords(); //获得查询记录的结果

        pageResult.setRows(orderList);
        return pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
    }

    @Override
    public List<Orders> selectOrderList(PageResult pageResult) {
        if (pageResult.getQuery() == null){
            return ordersMapper.selectList(null);
        }
        Integer pageNum = pageResult.getPageNum();
        Integer pageSize = pageResult.getPageSize();
        Long total = pageResult.getTotal();
        long size = pageSize * (pageNum - 1);

        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.like("login_name", pageResult.getQuery())
                .last("limit " + size +","  + pageResult.getPageSize());
        return ordersMapper.selectList(ordersQueryWrapper);
    }

    @Override
    public Orders selectOrderById(Integer id) {
        return ordersMapper.selectById(id);
    }
}
